/*********************************************************************************
  *Copyright (c)    2021   xldeng
  *FileName:        19.cpp.c
  *Author:          xldeng
  *Version:         1.0
  *Date:            2021/2/9 10:07
  *Description:     
  *Others:          
  *Function List:   
     1.…………
  *History:  
     1.Date:
       Author:
       Modification:
**********************************************************************************/
/*
 * 给你一个链表，删除链表的倒数第 n 个结点，并且返回链表的头结点。
 */
#include "../header.h"
class Solution {
public:
    ListNode* removeNthFromEnd(ListNode* head, int n) {
        ListNode *first = head, *second = head;
        for (int i = 0; i < n; ++i)
            first = first->next;
        if (first == nullptr)
            return head->next;
        while (first ->next != nullptr){
            first = first->next;
            second = second->next;
        }
        second->next = second->next->next;
        return head;
    }
};